import md5 from 'blueimp-md5'
import { encryptd, encryptd2 } from '@/utils/tools'

export default function (options, isEncryptd2 = false) {
  options = options ? options : {}
  let uid = options.uid || ''
  var result = {}
  var canvas = document.createElement('canvas')
  canvas.width = 2000
  canvas.height = 200
  canvas.style.display = 'inline'
  var ctx = canvas.getContext('2d')
  ctx.rect(0, 0, 10, 10)
  ctx.rect(2, 2, 6, 6)
  result.canvasWinding = ctx.isPointInPath(5, 5, 'evenodd') === false ? 'yes' : 'no'

  ctx.textBaseline = 'alphabetic'
  ctx.fillStyle = '#f60'
  ctx.fillRect(125, 1, 62, 20)
  ctx.fillStyle = '#069'
  if (options.dontUseFakeFontInCanvas) {
    ctx.font = '11pt Arial'
  } else {
    ctx.font = '11pt no-real-font-123'
  }
  ctx.fillText('Cwm fjordbank glyphs vext quiz, \ud83d\ude03', 2, 15)
  ctx.fillStyle = 'rgba(102, 204, 0, 0.2)'
  ctx.font = '18pt Arial'
  ctx.fillText('Cwm fjordbank glyphs vext quiz, \ud83d\ude03', 4, 45)

  ctx.globalCompositeOperation = 'multiply'
  ctx.fillStyle = 'rgb(255,0,255)'
  ctx.beginPath()
  ctx.arc(50, 50, 50, 0, Math.PI * 2, true)
  ctx.closePath()
  ctx.fill()
  ctx.fillStyle = 'rgb(0,255,255)'
  ctx.beginPath()
  ctx.arc(100, 50, 50, 0, Math.PI * 2, true)
  ctx.closePath()
  ctx.fill()
  ctx.fillStyle = 'rgb(255,255,0)'
  ctx.beginPath()
  ctx.arc(75, 100, 50, 0, Math.PI * 2, true)
  ctx.closePath()
  ctx.fill()
  ctx.fillStyle = 'rgb(255,0,255)'
  ctx.arc(75, 75, 75, 0, Math.PI * 2, true)
  ctx.arc(75, 75, 25, 0, Math.PI * 2, true)
  ctx.fill('evenodd')

  if (canvas.toDataURL) {
    result.rawData = canvas.toDataURL()
    let a1 = md5(result.rawData)
    let a2 = uid ? uid : new Date().getTime()
    if (isEncryptd2) {
      result.hash = encryptd2(`${a1}_${navigator.buildID}_${navigator.productSub}_${a2}`)
    } else {
      result.hash = encryptd(`${a1}_${navigator.buildID}_${navigator.productSub}_${a2}`)
    }
  }
  return result
}
